# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import grpc

from protos import helloworld_pb2
from protos import helloworld_pb2_grpc


from flask import Flask, request

import json

app = Flask(__name__)
channel = grpc.insecure_channel('localhost:50051')
stub = helloworld_pb2_grpc.GreetingStub(channel)


@app.route('/<int:id>')
def query(id):
    response = stub.SayHello(helloworld_pb2.HelloRequest(id=id))
    return response.message


@app.route('/create', methods=['POST'])
def create():
    data = json.loads(request.data)
    name = data.get('name', '')

    response = stub.CreateUser(helloworld_pb2.UserRequest(name=name))
    print response, type(response)

    return str(response)


def main():
    app.run(debug=True)

if __name__ == '__main__':
    main()
